I have a JS script which is failing when evaluated using Nashorn engine from Java. This code works fine on Java 8 but fails on Java 11. I am getting an error:
unknown call type GET:PROPERTY|ELEMENT|METHOD:NODE_PATH(Object)int@jdk.nashorn.internal.scripts.Script$Recompilation$20$11678$\^eval\_
java.lang.AssertionError: unknown call type GET:PROPERTY|ELEMENT|METHOD:NODE_PATH(Object)int@jdk.nashorn.internal.scripts.Script$Recompilation$20$11678$\^eval\_
at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.linker.NashornBottomLinker.linkBean(NashornBottomLinker.java:126)
at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.linker.NashornBottomLinker.getGuardedInvocation(NashornBottomLinker.java:78)
at jdk.dynalink/jdk.dynalink.linker.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:109)
The code is:
function __toCall() {
return require('nashorn-invoker')(
function (require) {
var Text = function Text() {
StringLeaf.call(this);
};
extend(Text, StringLeaf);
Text.prototype.ontologyType = function () {
return 'cmd.ontology.types.Text';
};
var BasicTest = function BasicTest() {
var _field = new Text();
_field._fieldName = 'field';
_field._name = 'Field';
_field._defaultValue = '';
var curNode = this;
Structure.call(this, {
field: _field
});
};
extend(BasicTest, Structure);
BasicTest.prototype.ontologyType = function () {
return 'cmd.ontology.test.BasicTest';
};
BasicTest.prototype._name = 'Basic Test';
return (function () {
var model = [];
var callbackFn = function (result) {
if (result !== true) model.push.apply(model, result);
};
test.child('field').validate(vc, callbackFn);
var result = model.length === 0 ? true : model;
print('Callback function result: ' + JSON.stringify(result));
return result;
})();
}
);
}
File nashorn-invoker.js
module.exports = function nashornInvoker(callback) {
return callback(require); // runs the given callback and provide our "require" context
};
Similar problem was answered here.
JS script doesn't run with Java 11 but works with Java 8
This sounds like it could be the JDK-8261926 bug. The good news is that this is fixed, but only in the standalone Nashorn, not the one integrated into Java 11. There's also a page describing how you can ensure that standalone Nashorn is used by your program instead of the integrated one on Java 11.